<?php

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

// Don't load if optionsframework_init is already defined
if (is_admin() && ! function_exists( 'optionsframework_init' ) ) :

function optionsframework_init() {
    
	//  If user can't edit theme options, exit
	if ( ! current_user_can( 'edit_theme_options' ) ) return false;
    
	// Loads the required Options Framework classes.
	require plugin_dir_path( __FILE__ ) . 'includes/class-options-framework.php';
	require plugin_dir_path( __FILE__ ) . 'includes/class-options-framework-admin.php';
	require plugin_dir_path( __FILE__ ) . 'includes/class-options-interface.php';
    require plugin_dir_path( __FILE__ ) . 'includes/class-options-sanitization.php';

	// Instantiate the main plugin class.
	$options_framework = new Options_Framework;
	$options_framework->init();

	// Instantiate the options page.
	$options_framework_admin = new Options_Framework_Admin;
	$options_framework_admin->init();
}

add_action( 'init', 'optionsframework_init', 20 );

endif;


/**
 * Helper function to return the theme option value.
 * If no value has been saved, it returns $default.
 * Needed because options are saved as serialized strings.
 *
 * Not in a class to support backwards compatibility in themes.
 */

if ( ! function_exists( 'of_get_option' ) ) :

function of_get_option( $name, $default = false ) {
	$config = get_option( 'apollo_optionsframework' );
    
	if ( ! isset( $config['id'] ) ) {
		return $default;
	}

	$options = get_option( $config['id'] );

	if ( isset( $options[$name] ) ) {
		return $options[$name];
	}

	return $default;
}

endif;

if ( ! function_exists( 'render_php_to_string' ) ) :
    
    function render_php_to_string($file, $vars=null) {
        
        if (is_array($vars) && !empty($vars)) {
            extract($vars);
        }
        ob_start();
        include $file;
        return ob_get_clean();
    }

endif;

if ( ! function_exists( 'get_override_css_abs_path' ) ) :
    function get_override_css_abs_path() {

        return APOLLO_PARENT_DIR. '/sites/'. get_current_domain(). '/override_css.css';
    }
endif;

if ( !function_exists( 'get_primary_color_css_abs_path' ) ) :
    
    function get_primary_color_css_abs_path() {

        return APOLLO_PARENT_DIR. '/sites/'. get_current_domain() . '/primary_color_css.css';

    }

endif;

if ( !function_exists( 'get_override_css_url' ) ) :

    function get_override_css_url() {
    
        return get_template_directory_uri(). '/sites/'. get_current_domain() . '/override_css.css';
    }

endif;

if ( !function_exists( 'get_primary_color_css_url' ) ) :

    function get_primary_color_css_url() {
    
        return get_template_directory_uri(). '/sites/'. get_current_domain(). '/primary_color_css.css';

    }
endif;

if ( !function_exists('get_current_domain') ):
function get_current_domain() {
    $sites = get_blog_details(get_current_blog_id());
    return $sites->domain;
}
endif;


function apollo_get_header() {
    return html_entity_decode(of_get_option( 'apollo_header' ));
}

function apollo_get_footer() {
    return html_entity_decode(of_get_option( 'apollo_footer' ));
}

function apollo_get_site_info() {
    return html_entity_decode(of_get_option( 'apollo_site_info' ));
}

// Setup the WordPress core custom background feature.
add_theme_support( 'custom-background', apply_filters( 'apollo_custom_background_args', array(
    'default-color' => APOLLO_DEFAULT_BACKGROUND_COLOR
) ) );

if ( !function_exists( 'adjust_brightness' ) ) {
    
    function adjust_brightness( $hex, $steps = 20 ) {
        
        // Steps should be between -255 and 255. Negative = darker, positive = lighter
        $steps = max(-255, min(255, $steps));

        // Format the hex color string
        $hex = str_replace('#', '', $hex);
        
        if (strlen($hex) == 3) {
            $hex = str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2);
        }

        // Get decimal values
        $r = hexdec(substr($hex,0,2));
        $g = hexdec(substr($hex,2,2));
        $b = hexdec(substr($hex,4,2));

        // Adjust number of steps and keep it inside 0 to 255
        $r = max(0,min(255,$r + $steps));
        $g = max(0,min(255,$g + $steps));  
        $b = max(0,min(255,$b + $steps));

        $r_hex = str_pad(dechex($r), 2, '0', STR_PAD_LEFT);
        $g_hex = str_pad(dechex($g), 2, '0', STR_PAD_LEFT);
        $b_hex = str_pad(dechex($b), 2, '0', STR_PAD_LEFT);

        return '#'. $r_hex.$g_hex.$b_hex;
    }

}

if ( !function_exists( 'check_is_bright_color' ) ) {
    
    function check_is_bright_color( $hex )  {
    
        $hex = str_replace( '#' , '', $hex );
        $r = hexdec(substr($hex,0,2));
        $g = hexdec(substr($hex,2,2));
        $b = hexdec(substr($hex,4,2));
        return $r + $g + $b > 382;
    }
}

